/*
 *	This file is part of Lounge-Cast.
 *
 *  Author:  	Markus Moormann
 *	E-mail:  	mmoorman@uni-osnabrueck.de
 *	Copyright:	University of Osnabrueck (Zentrum Virtuos)
 * 	Website:	www.uni-osnabrueck.de
 *	Date:		16.02.2011
 *
 *	Lounge-Cast is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *	Lounge-Cast is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Lounge-Cast.  If not, see <http://www.gnu.org/licenses/>.
 */
package webservice.functions;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.ProduceMime;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;

import org.apache.log4j.Logger;

import webservice.dbConnection.ConnectionHelper;
import webservice.helper.HelperClass;

/**
 * WebService class for validating player data
 * 
 * @author Markus Moormann
 *
 */
@Path("/validate")
@ProduceMime("application/xml")
public class ValidationResource {

	/**
	 * a logger to log errors, debug information, etc.
	 */
	private static Logger LOG = Logger.getLogger(ValidationResource.class);
	
	/**
	 * validates a given apiKey and playerId 
	 * called as http://example.org/webservice/uploadedFiles/validate?playerId=&apiKey=
	 * 
	 * @param apiKey the apiKey to validate
	 * @param playerId the playerId to validate
	 * @return a XML containing following &lt;result&gt;[true|false]&lt;/result&gt; depending on the result of test
	 */
	@GET
	public String validate(@QueryParam("apiKey") @DefaultValue("") String apiKey,
						   @QueryParam("playerId") @DefaultValue("") String playerId) {
		boolean result = false;
		Connection connection = null;
		Statement statement = null;
		if(apiKey.equals(HelperClass.getProperty("apiKey"))) {
			try {
				connection = ConnectionHelper.getConnection();
				statement = connection.createStatement();
				
				String selectPlayer = 	"SELECT " +
											"player_id " +
										"FROM " +
											"player " +
										"WHERE " +
											"player_id = '" + playerId + "'";
				ResultSet rs = statement.executeQuery(selectPlayer);
				result = rs.next();
			} catch (SQLException e) {
				LOG.error(e);
				throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
			} finally {
				ConnectionHelper.close(connection);
				ConnectionHelper.close(statement);
			}
		}
		return "<result>"+result+"</result>";
	}
}
